﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security;
using System.Web;
using System.Web.Routing;
using Rhino.Mocks;

namespace Web7.Tests.Extensions
{
    /// <summary>
    /// 创建模拟HTTP上下文,支持其他情况下测试
    /// </summary>
    public class FakeHttpContextFactory
    {

        /// <summary>
        /// Mocking httocontxt测试
        /// </summary>
        /// <param name="fullUrl"></param>
        [SecuritySafeCritical]
        public FakeHttpContextFactory(Uri fullUrl)
        {
            CreateContext(fullUrl);
        }

        /// <summary>
        /// Mocking httocontxt测试
        /// </summary>
        /// <param name="path">路径</param>
        [SecuritySafeCritical]
        public FakeHttpContextFactory(string path)
        {
            CreateContext(new Uri("http://mysite" + VirtualPathUtility.ToAbsolute(path, "/")));
        }

        /// <summary>
        /// Mocking httocontxt测试
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="routeData">路由</param>
        [SecuritySafeCritical]
        public FakeHttpContextFactory(string path, RouteData routeData)
        {
            CreateContext(new Uri("http://mysite" + VirtualPathUtility.ToAbsolute(path, "/")), routeData);
        }

        public HttpContextBase HttpContext { get; private set; }
        public RequestContext RequestContext { get; private set; }

        /// <summary>
        /// mocking HTTP上下文来测试
        /// </summary>
        /// <param name="fullUrl"></param>
        /// <param name="routeData"></param>
        /// <returns></returns>
        private void CreateContext(Uri fullUrl, RouteData routeData = null)
        {
            //Request context

            RequestContext = MockRepository.GenerateMock<RequestContext>();

            //Request

            var request = MockRepository.GenerateMock<HttpRequestBase>();
            request.Stub(x => x.AppRelativeCurrentExecutionFilePath).Return("~" + fullUrl.AbsolutePath);
            request.Stub(x => x.PathInfo).Return(string.Empty);
            request.Stub(x => x.RawUrl).Return(VirtualPathUtility.ToAbsolute("~" + fullUrl.AbsolutePath, "/"));
            request.Stub(x => x.RequestContext).Return(RequestContext);
            request.Stub(x => x.Url).Return(fullUrl);
            request.Stub(x => x.ApplicationPath).Return("/");
            request.Stub(x => x.Cookies).Return(new HttpCookieCollection());

            //Cache
            var cache = MockRepository.GenerateMock<HttpCachePolicyBase>();

            //Response 
            //var response = new FakeHttpResponse();
            var response = MockRepository.GenerateMock<HttpResponseBase>();
            response.Stub(x => x.ApplyAppPathModifier(null)).IgnoreArguments().Do(new Func<string, string>(appPath => appPath));
            response.Stub(x => x.Cache).Return(cache);

            //Server

            var server = MockRepository.GenerateStub<HttpServerUtilityBase>();
            server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return(Environment.CurrentDirectory);

            //HTTP Context

            HttpContext = MockRepository.GenerateMock<HttpContextBase>();
            HttpContext.Stub(x => x.Cache).Return(HttpRuntime.Cache);
            HttpContext.Stub(x => x.Items).Return(new Dictionary<object, object>());
            HttpContext.Stub(x => x.Request).Return(request);
            HttpContext.Stub(x => x.Server).Return(server);
            HttpContext.Stub(x => x.Response).Return(response);

            RequestContext.Stub(x => x.HttpContext).Return(HttpContext);

            if (routeData != null)
            {
                RequestContext.Stub(x => x.RouteData).Return(routeData);
            }
        }

    }
}
